package utils

import (
	"encoding/json"
	"net/http"
)

type H struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
	Rows int         `json:"rows"`
}

func Resp(w http.ResponseWriter, code int, data interface{}, msg string) {

}

func RespFail(w http.ResponseWriter, msg string) {
	Resp(w, -1, nil, msg)
}

func RespOk(w http.ResponseWriter, data interface{}) {
	Resp(w, 0, data, "ok")
}

func RespOKList(w http.ResponseWriter, data []interface{}, total int) {
	RespList(w, data, "ok")
}

func RespList(w http.ResponseWriter, data []interface{}, msg string) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	h := H{
		Code: 0,
		Msg:  msg,
		Data: data,
		Rows: len(data),
	}
	ret, err := json.Marshal(h)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	_, err = w.Write(ret)
}
